<?php $this->headScript()->appendFile($this->baseUrl() . '/application/modules/Avatar/externals/webcam/webcam.js') ?>
<?php
    $script = "
        var tmp_delay = 3;
        var interval = '';
               function cancel_3freeze() {
                    $('wait_freeze').set('text', 3);
                    $('full_wait_freeze').setStyle('display', 'none');
                    clearInterval(interval);
                    do_reset();
               }
               function do_freeze() {
                    $('webcam_reset').setStyle('display', 'inline');
                    $('webcam_upload').setStyle('display', 'inline');
                    $('webcam_freeze').setStyle('display', 'none');
                    $('webcam_3freeze').setStyle('display', 'none');
                    $('cancel_delay').setStyle('display', 'none');
                    webcam.freeze();
               }
               function do_3freeze() {                    
                    $('full_wait_freeze').setStyle('display', 'inline');
                    $('webcam_freeze').setStyle('display', 'none');
                    $('webcam_3freeze').setStyle('display', 'none');
                    $('cancel_delay').setStyle('display', 'inline');
                    tmp_delay = 3;
                    interval=setInterval(function(){
                                                    delay_freeze();
                                                    },1000);
               }
               function delay_freeze() {
                    var wait_freeze = $('wait_freeze');
                    if (tmp_delay > 0) {
                        tmp_delay = tmp_delay - 1;                     
                        wait_freeze.set('text', wait_freeze.get('text') - 1);
                    }
                    else {
                        wait_freeze.set('text', 3);
                        $('full_wait_freeze').setStyle('display', 'none');
                        clearInterval(interval);
                        do_freeze();
                    }
               }
               function do_reset() {
                    $('webcam_reset').setStyle('display', 'none');
                    $('webcam_upload').setStyle('display', 'none');
                    $('webcam_freeze').setStyle('display', 'inline');
                    $('webcam_3freeze').setStyle('display', 'inline');
                    $('cancel_delay').setStyle('display', 'none');
                    webcam.reset();
               }
               function do_upload() {
                    $('webcam_button').setStyle('display', 'none');
                    $('webcam_do_upload').setStyle('display', 'block');
                    webcam.upload();
               }
               function my_completion_handler(msg) {
                    msg = JSON.decode(msg);
                    // extract URL out of PHP output
                    if (\$type(msg)!='object' || !msg.result || msg.result !=true) {
                            if (msg.message != null) alert(msg.message);
                    }
                    else {                       
                       parent.window.location.reload();
                    }
               }
               webcam.set_api_url( '" . $this->url(array('module' => 'avatar', 'controller' => 'edit', 'action' => 'savewebcam'), 'default') . "' );
               webcam.set_swf_url( '" . $this->baseUrl() . '/application/modules/Avatar/externals/webcam/webcam.swf' . "' )
               webcam.set_quality( 100 );
			   webcam.set_hook( 'onComplete', 'my_completion_handler' );
               webcam.set_shutter_sound( true, '" . $this->baseUrl() . '/application/modules/Avatar/externals/webcam/shutter.mp3' . "' );";
    $this->headScript()->appendScript($script, $type = 'text/javascript', $attrs = array());
	
?>
<div style="padding:10px 10px 0 13px;">
<h3>Take a WebCam Snapshot</h3>
<?php if (isset($this->error)): ?>
<p><?php echo $this->error ?></p>
<?php endif; ?>
<script type="text/javascript">
        document.write( webcam.get_html(320, 240) );
</script>
<form id="webcam_button">
        <button type=button onClick="javascript:webcam.configure()" style="display:none">Configure...</button>
        <button id="webcam_freeze" type=button onClick="javascript:do_freeze()">Take Snapshot</button>
        <button id="webcam_3freeze" type=button onClick="javascript:do_3freeze()">Take Snapshot in 3 sec</button>
        <span id="full_wait_freeze" style="display:none"><span id="wait_freeze">3</span></span>
        <button id="cancel_delay" type=button onClick="javascript:cancel_3freeze()" style="display: none;">Cancel</button>

        <button id="webcam_reset" style="display: none;" type=button onClick="do_reset()">Reset</button>
        <button id="webcam_upload" style="display: none;" type=button onClick="javascript:do_upload()">Save</button> or <a onclick="parent.Smoothbox.close();" href="javascript:void(0);" type="button" id="cancel" name="cancel">Close</a>
</form>
<span id="webcam_do_upload" style="display: none;">Uploading...</span>
</div>
